<?php

namespace App\Console\Commands;

use App\Models\OldUser;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MigrateUser extends Command
{
    protected $signature = 'script:migrate-user';

    protected $description = '迁移用户数据';

    protected $newUserConnectionName = 'dev_user';

    public function __construct()
    {
        parent::__construct();

        //清空新数据库表
//        DB::connection($this->newUserConnectionName)->statement('TRUNCATE table `jzc_user`;');
//        DB::connection($this->newUserConnectionName)->statement('TRUNCATE table `jzc_user_address`;');
//        DB::connection($this->newUserConnectionName)->statement('TRUNCATE table `jzc_user_email`;');
//        DB::connection($this->newUserConnectionName)->statement('TRUNCATE table `jzc_user_account`;');
//        DB::connection($this->newUserConnectionName)->statement('TRUNCATE table `jzc_user_info`;');
//        DB::connection($this->newUserConnectionName)->statement('TRUNCATE table `jzc_user_phone`;');
    }

    public function handle()
    {
        Log::info('用户迁移开始');

        $minUserid = 0;

        while (true) {

            //获取老数据库用户数据 每次获取1000条
            $oldUsers = OldUser::where('userid', '>', $minUserid)->orderBy('userid')->limit(1000)->get();

            if ($oldUsers->isEmpty()) {
                break;
            }

            //遍历写入到新数据库
            $oldUsers->each(function ($oldUser) {

                Log::info('用户id：', [$oldUser->userid]);

                $oldUser->create_time = strtotime($oldUser->create_time);

                DB::beginTransaction();

                try {
                    //写入用户表
                    $this->writeUser($oldUser);

                    //写入账户表
                    $this->writeUserAccount($oldUser);

                    //写入地址表
                    $this->writeUserAddress($oldUser);

                    //写入邮箱表
                    $this->writeUserEmail($oldUser);

                    //写入手机号管理表
                    $this->writeUserPhone($oldUser);

                    //写入用户信息表
                    $this->writeUserInfo($oldUser);

                    DB::commit();
                } catch (\Exception $exception) {

                    DB::rollBack();

                    Log::error('该用户迁移失败', [
                        'userid' => $oldUser->userid,
                        'exception' => $exception->getCode() . $exception->getMessage()
                    ]);

                    Log::info('用户迁移完成');exit;
                }
            });

            $minUserid = $oldUsers->last()->userid;
        }

        Log::info('用户迁移完成');
    }

    public function writeUser($oldUser)
    {

        $user = [
            'id' => $oldUser->userid,
            'last_login_time' => strtotime($oldUser->login_time),
            'create_time' => $oldUser->create_time,
            'update_time' => $oldUser->create_time
        ];

        DB::connection($this->newUserConnectionName)->table('user')->insert($user);
    }

    public function writeUserAccount($oldUser)
    {
        $user =  [
            'user_id' => $oldUser->userid,
            'username' => $oldUser->phone,
            'password' => (string) $oldUser->pwd,
            'create_time' => $oldUser->create_time,
            'update_time' => $oldUser->create_time
        ];

        DB::connection($this->newUserConnectionName)->table('user_account')->insert($user);
    }

    public function writeUserAddress($oldUser)
    {
        $userid = $oldUser->userid;

        $oldUser->addrs->each(function ($address) use ($userid) {

            $province = explode('-', $address->province);

            $address->create_time = strtotime($address->create_time);

            if ($address->type == 1) {
                $address->type = 2;
            }

            if ($address->type == 2) {
                $address->type = 1;
            }

            $row = [
                'user_id' => $userid,
                'receiver_name' => $address->name,
                'receiver_phone' => $address->phone,
                'prov' => $province[0],
                'city' => $province[1],
                'area' => $province[2],
                'addr' => $address->addr,
                'is_default' => $address->is_default,
                'type' => $address->type,
                'create_time' => $address->create_time,
                'update_time' => $address->create_time,
                'is_deleted' => $address->is_del
            ];

            DB::connection($this->newUserConnectionName)->table('user_address')->insert($row);
        });
    }

    public function writeUserEmail($oldUser)
    {
        $user = [
            'user_id' => $oldUser->userid,
            'email' => (string) $oldUser->email,
            'create_time' => $oldUser->create_time,
            'update_time' => $oldUser->create_time
        ];

        DB::connection($this->newUserConnectionName)->table('user_email')->insert($user);
    }

    public function writeUserPhone($oldUser)
    {
        $user = [
            'user_id' => $oldUser->userid,
            'phone' => $oldUser->phone,
            'create_time' => $oldUser->create_time,
            'update_time' => $oldUser->create_time
        ];

        DB::connection($this->newUserConnectionName)->table('user_phone')->insert($user);
    }

    public function writeUserInfo($oldUser)
    {
        $user = [
            'user_id' => $oldUser->userid,
            'avatar' => $oldUser->headimgurl,
            'create_time' => $oldUser->create_time,
            'update_time' => $oldUser->create_time
        ];

        DB::connection($this->newUserConnectionName)->table('user_info')->insert($user);
    }
}
